<?php

namespace app\common\service;

use Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use think\facade\Log;

class MailService
{
    public static function send($filename)
    {
        $mail = new PHPMailer(true);
        try {
            //Server settings
            $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
            $mail->isSMTP();                                            //Send using SMTP
            $mail->Host = 'smtp.qq.com';                     //Set the SMTP server to send through
            $mail->SMTPAuth = true;                                   //Enable SMTP authentication
            $mail->Username = env('mail_username');                     //SMTP username
            $mail->Password = env('mail_password');                               //SMTP password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
            $mail->Port = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
            //接收方
            $mail->setFrom(env('mail_username'), '今日数据推送');
            $mail->addAddress(env('mail_address'), '目标接收方');
            //附件
            $mail->addAttachment(env('root_path') . 'public/data/' . $filename . '.xlsx');         //Add attachments
            //内容
            $mail->isHTML(true);                                  //Set email format to HTML
            $mail->Subject = $filename;
            $mail->Body = date('Y-m-d') . '数据报表';
            $mail->send();
        } catch (Exception $e) {
            Log::error($mail->ErrorInfo);
        }
    }

}